﻿using Financial.Entity;
using Financial.Entity.Enums;
using System.Collections.Generic;
using System.Linq;
using EntityFramework.Extensions;
using EntityFramework.Future;
using System.Data.Entity;

namespace Financial.Data
{
    public class IncomeDL
    {
        FLUXEntities context = new FLUXEntities();
        public List<Income> GetPage(int pageIndex, int pageSize,
            out int totalCount, SortOrder sortOrder, IncomeType? incomeType)
        {
            var countFuture = context.Incomes.FutureCount();
            FutureQuery<Income> incomeFuture = null;

            switch (incomeType)
            {
                case IncomeType.Wage:
                    if (sortOrder == SortOrder.asc)
                    {
                        incomeFuture = context.Incomes.OrderBy(i => i.Wage).Future();
                    }
                    else
                    {
                        incomeFuture = context.Incomes.OrderByDescending(i => i.Wage).Future();
                    }
                    break;

                case IncomeType.FullName:
                    if (sortOrder == SortOrder.asc)
                    {
                        incomeFuture = context.Incomes.OrderBy(i => i.FamilyMember.FirstName).Future();
                    }
                    else
                    {
                        incomeFuture = context.Incomes.OrderByDescending(i => i.FamilyMember.FirstName).Future();
                    }
                    break;

                case IncomeType.MealTicket:
                    if (sortOrder == SortOrder.asc)
                    {
                        incomeFuture = context.Incomes.OrderBy(i => i.MealTicket).Future();
                    }
                    else
                    {
                        incomeFuture = context.Incomes.OrderByDescending(i => i.MealTicket).Future();
                    }
                    break;

                case IncomeType.Bonus:
                    if (sortOrder == SortOrder.asc)
                    {
                        incomeFuture = context.Incomes.OrderBy(i => i.Bonus).Future();
                    }
                    else
                    {
                        incomeFuture = context.Incomes.OrderByDescending(i => i.Bonus).Future();
                    }
                    break;

                case IncomeType.Date:
                    if (sortOrder == SortOrder.asc)
                    {
                        incomeFuture = context.Incomes.OrderBy(i => i.Date).Future();
                    }
                    else
                    {
                        incomeFuture = context.Incomes.OrderByDescending(i => i.Date).Future();
                    }
                    break;

                default:
                    incomeFuture = context.Incomes.OrderByDescending(i => i.Date).Future();
                    break;
            }

            totalCount = countFuture.Value;
            return incomeFuture.ToList();
        }

        public void CreateSaveToDatabase(Income income)
        {
            context.Incomes.Add(income);
            context.SaveChanges();
        }

        public void EditSaveToDatabase(Income income)
        {
            context.Entry(income).State = EntityState.Modified;
            context.SaveChanges();
        }

        public List<FamilyMember> AccessFamilyMembers()
        {
            return context.FamilyMembers.ToList();
        }

        public Income FindIncome(int id)
        {
            return context.Incomes.Find(id);
        }
    }
}
